﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 类和对象
{
    internal class MySupermarket
    {
        public Product[] products = new Product[100];
        int count = 0;
        public MySupermarket() { }
        public void ShowMenu()
        {
            Console.WriteLine("欢迎使用我的超市");
            bool isStarted = true;


            while (isStarted)
            {
                Console.WriteLine("请选择操作编号：");
                Console.WriteLine("     1.录入商品");
                Console.WriteLine("     2.查看商品");
                Console.WriteLine("     3.退出系统");

                int num = Convert.ToInt32(Console.ReadLine());
                switch (num)
                {
                    case 1:
                        AddProduct(products, ref count);
                        break;
                    case 2:
                        ShowProduct(products, count);
                        break;
                    case 3:
                        Console.WriteLine("欢迎下次使用\n");
                        isStarted = false;
                        break;
                    default:
                        Console.WriteLine("没有输入的编号\n");
                        break;
                }
            }
        }

        public void AddProduct(Product[] products, ref int count)
        {
            while(true)
            {
                Console.WriteLine("请输入商品编号: ");
                int id = Convert.ToInt32(Console.ReadLine());
                if (id == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("输入的编号不能为0!");
                    Console.ResetColor();
                    continue;
                }

                Console.WriteLine("请输入商品名称: ");
                string name = Console.ReadLine();
                if (name == "")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("输入的名称为空!");
                    Console.ResetColor();
                    continue;
                }

                Console.WriteLine("请输入商品价格: ");
                double price = Convert.ToDouble(Console.ReadLine());
                if (price == 0.0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("输入的价格为空!");
                    Console.ResetColor();
                    continue;
                }
                //描述可以没有
                Console.WriteLine("请输入商品描述: ");
                string description = Console.ReadLine();
                Console.WriteLine();

                Product product = new Product(id, name, price, description);
                products[count++] = product;
                break;
            }
        }
        public void ShowProduct(Product[] products, int count)
        {
            Console.WriteLine("商品编号\t商品名称\t商品价格\t商品描述");
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(products[i].id + "\t\t" + products[i].name + "\t" + products[i].price + "\t\t" + products[i].description);
            }
            Console.WriteLine();
        }
    }
}
